package com.bjpowernode.controller;

import com.bjpowernode.beans.User;
import com.bjpowernode.exception.MyLoginException;
import com.bjpowernode.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserServices userServices;

    @RequestMapping("in.action")
    public HashMap<String, Object> in(HttpServletRequest res, HttpServletResponse resp, String loginAct, String loginPwd, boolean rememberMe) throws MyLoginException {

        System.out.println("rememberMe:" + rememberMe);
        System.out.println("loginAct:" + loginAct);
        System.out.println("loginPwd:" + loginPwd);
        User userTemp = userServices.login(loginAct, loginPwd);
        HttpSession session = res.getSession();
        Object user = session.getAttribute("user");
        System.out.println("session:" + user);
        if (userTemp == null) {
            throw new MyLoginException("用户名或者密码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "success");
        //成功之后
        System.out.println("登录时候的HttpSession:" + session.getId());
        //把登录成功的user信息方到session里面
        session.setAttribute("user", userTemp);

        //如果有10天免登录，则必须要求浏览器记录住登录用户名和登录密码
        //记到cookies里面
        if (rememberMe) {
            //cookie对象的属性名称类型是字符串
            //cookie对象的属性名称对应的值的类型必须是字符串
            //创建用户账号cookies对象
            Cookie loginActCookie = new Cookie("loginAct", loginAct);
            //设置cookies存活的时间(10天)
            loginActCookie.setMaxAge(10 * 24 * 60 * 60);
            //cookie对地址的作用域设置（也就是那个地址可以使用cookies）
            //如果所有页面都可以使用这个cookies则直接设置作用域为根
            loginActCookie.setPath("/");
            //创建密码cookies对象
            Cookie loginPwdCookie = new Cookie("loginPwd", loginPwd);
            loginPwdCookie.setMaxAge(10 * 24 * 60 * 60);
            loginPwdCookie.setPath("/");
            //需要把cookies对象发送到浏览器，并且通知浏览器创建cookies数据（浏览器不能关闭cookies）
            //这里需要响应对象（HttpServletResponse）发送
            resp.addCookie(loginActCookie);
            resp.addCookie(loginPwdCookie);
        }
        return map;
    }


    @RequestMapping("out.action")
    public HashMap<String, Object> out(HttpServletRequest res, HttpServletResponse resp) {
        HttpSession session = res.getSession();
        //删除session里面的数据
        session.removeAttribute("user");
        //删除cookies里面的数据
        Cookie loginActCookie = new Cookie("loginAct", "");
        //设置cookies存活的时间(0)
        loginActCookie.setMaxAge(0);
        loginActCookie.setPath("/");
        Cookie loginPwdCookie = new Cookie("loginPwd", "");
        loginPwdCookie.setMaxAge(0);
        loginPwdCookie.setPath("/");
        resp.addCookie(loginActCookie);
        resp.addCookie(loginPwdCookie);
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "success");
        return map;
    }

    @RequestMapping("userInfo.action")
    public HashMap<String, Object> userInfo(HttpServletRequest res, HttpServletResponse resp) {
        HttpSession session = res.getSession();
        User user =(User)session.getAttribute("user");
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "success");
        map.put("data",user);
        return map;
    }
}
